﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using SIMA.Interfaces;
using SIMA.Core.Data.Access;
using SIMA.Core.Data.Collection;
using SIMA.Controls;
using SIMA.Core.Enums;
using SIMA.Entities;

namespace SIMA.Client.Auxiliars.MVVM
{
    public class AdminPerfilMvvm : AccessClass<T_C_Perfil>
    {
        #region Consts

        private const string IDTABLA = "13";

        #endregion

        #region Fields

        private List<T_C_Perfil> perfiles;
        private List<T_C_Estado> estados;
        private SmartContainer<T_C_Perfil> PerfilSmart;
        private SmartContainer<T_C_Estado> estadosSmart;
        private FilterType filtro;

        #endregion

        #region Properties

        public List<T_C_Perfil> Perfiles
        {
            get { return perfiles; }
            set
            {
                perfiles = value;
                this.RaisePropertyChanged("Perfiles");
            }
        }

        public List<T_C_Estado> Estados
        {
            get
            {
                return estados;
            }
            set
            {
                estados = value;
                this.RaisePropertyChanged("Estados");
            }
        }

        public FilterType Filtro
        {
            get
            {
                return filtro;
            }
            set
            {
                filtro = value;
                this.RaisePropertyChanged("Filtro");
                SelectSpecialized();
            }
        }

        #endregion

        #region Constructor

        public AdminPerfilMvvm()
        {
            Init();
        }

        #endregion

        #region Destructor

        ~AdminPerfilMvvm()
        {
            this.Dispose();
        }
        #endregion

        #region Private Methods

        private void Init()
        {
            DataAccess.SetConnection(SIMA.Core.Enums.AccessType.FromString, CADCONNECTIONSTRING);
            PerfilSmart = new SmartContainer<T_C_Perfil>();
            PerfilSmart.Inserted += new EventHandler<SIMA.Core.EventArgs.InsertedEventArgs>(smartPerfil_Inserted);
            PerfilSmart.Deleted += new EventHandler<SIMA.Core.EventArgs.DeletedEventArgs>(smartPerfil_Deleted);
            PerfilSmart.Updated += new EventHandler<SIMA.Core.EventArgs.UpdatedEventArgs>(smartPerfil_Updated);
            PerfilSmart.Failed += new EventHandler<SIMA.Core.EventArgs.FailedEventArgs>(smartPerfil_Failed);
            estadosSmart = new SmartContainer<T_C_Estado>(false);
            StringBuilder strBuilder = new StringBuilder();
            strBuilder.Append("Id_Tabla = ");
            strBuilder.Append(IDTABLA);
            Estados = estadosSmart[strBuilder.ToString(), CommandType.CommandText];
            Filtro = FilterType.Active;
        }

        void smartPerfil_Updated(object sender, SIMA.Core.EventArgs.UpdatedEventArgs e)
        {
            SIMAMessageBox.Show(e.InstantUpdated.ToLongTimeString(), "Actualización Realizada.", e.QueryString, SIMAMessageBoxButtons.OK, SIMAMessageBoxImage.Information);
            SelectSpecialized();
        }

        void smartPerfil_Deleted(object sender, SIMA.Core.EventArgs.DeletedEventArgs e)
        {
            SIMAMessageBox.Show(e.InstantDeleted.ToLongTimeString(), "Eliminación Realizada.", e.QueryString, SIMAMessageBoxButtons.OK, SIMAMessageBoxImage.Information);
            SelectSpecialized();
        }

        void smartPerfil_Inserted(object sender, SIMA.Core.EventArgs.InsertedEventArgs e)
        {
            SIMAMessageBox.Show(e.InstantInserted.ToLongTimeString(), "Registro Realizado", e.QueryString, SIMAMessageBoxButtons.OK, SIMAMessageBoxImage.Information);
            SelectSpecialized();
        }

        void smartPerfil_Failed(object sender, SIMA.Core.EventArgs.FailedEventArgs e)
        {
            SIMAMessageBox.Show(e.InstantFailed.ToLongTimeString(), e.ExceptionOcurred.Message, e.QueryString, SIMAMessageBoxButtons.OK, SIMAMessageBoxImage.Error);
        }

        #endregion

        #region Public Methods

        public override void Register(T_C_Perfil item)
        {
            PerfilSmart.Add(item);
        }

        public override void Delete(T_C_Perfil item)
        {
            PerfilSmart.Remove(item);
        }

        public override void Update(T_C_Perfil itemToUpdate, T_C_Perfil itemUpdated)
        {
            PerfilSmart.Update(itemToUpdate, itemUpdated);
        }

        public override void SelectAll()
        {
            PerfilSmart.SelectAll();
            Perfiles = PerfilSmart.ToList();
        }

        public void SelectSpecialized()
        {
            switch (Filtro)
            {
                case FilterType.Active:
                    Perfiles = PerfilSmart["SelectActivePerfil", CommandType.StoredProcedure];
                    break;
                case FilterType.Inactive:
                    Perfiles = PerfilSmart["SelectInactivePerfil", CommandType.StoredProcedure];
                    break;
            }
        }

        #endregion
    }
}